<!DOCTYPE html>
<html>
<head lang="zh-cmn-Hans">
    <meta charset="UTF-8">
    <title>DSBridge Test</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
    <script src="./dsbridge.js"></script>
</head>
<style>
    .btn2 {
        text-align: center;
        background: #d8d8d8;
        color: #222;
        padding: 20px;
        margin: 30px;
        font-size: 24px;
        border-radius: 4px;
        box-shadow: 4px 2px 10px #999;
    }

    .btn:active {
        opacity: .7;
        box-shadow: 4px 2px 10px #555;
    }
 .btn {
        text-align: center;
        background: #d8d8d8;
        color: #222;
        padding: 5px;
        margin: 5px;
        font-size: 24px;
        border-radius: 4px;
        box-shadow: 4px 2px 10px #999;
    }

</style>
<body>
<div><input class="btn" type="button" onclick="getToken()" value="获取Token">
    <input class="btn" type="button" onclick="callSyn()" value="同步返值">
    <input class="btn" type="button" onclick="callAsyn()" value="异步返值">
    <input class="btn" type="button" onclick="callNoArgSyn()" value="同步没有入参">
    <input class="btn" type="button" onclick="callNoArgAsyn()" value="异步没有入参">

</div>
<div><input class="btn" type="button" onclick="echoSyn()" value="echo.Syn">
    <input class="btn" type="button" onclick="echoAsyn()" value="echo.Asyn">
    <input class="btn" type="button" onclick="callProgress()" value="callProgress">

    <input class="btn" type="button" onclick="hasNativeMethod('xx')" value="hasNativeMethod('xx')">
    <input class="btn" type="button" onclick="callAsyn_()"
           value="Stress test，2K times consecutive asynchronous API calls">

</div>

<div class="btn2" onclick="callNever()">Never call because without @JavascriptInterface
    annotation<br/>( This test is
    just for Android ,should be ignored in IOS )
</div>
<div class="btn2" onclick="hasNativeMethod('testSyn')">hasNativeMethod("testSyn")</div>

<script>
function getToken(){
 alert(dsBridge.call("getToken", ""))
}
    function callSyn() {
        alert(dsBridge.call("testSyn", "testSyn"))
    }

    function callAsyn() {
        dsBridge.call("testAsyn","testAsyn11111", function (v) {
            alert(v)
        })
    }

    function callAsyn_() {
        for (var i = 0; i < 2000; i++) {
            dsBridge.call("testAsyn", "js+" + i, function (v) {
                if (v == "js+1999 [ asyn call]") {
                    alert("All tasks completed!")
                }
            })
        }
    }

    function callNoArgSyn() {
        alert(dsBridge.call("testNoArgSyn"));
    }

    function callNoArgAsyn() {
        dsBridge.call("testNoArgAsyn", function (v) {
            alert(v)
        });
    }

    function callNever() {
        alert(dsBridge.call("testNever", {msg: "testSyn"}))
    }

    function echoSyn() {
        // call function with namespace
        var ret=dsBridge.call("echo.syn",{msg:" I am echoSyn call", tag:1});
        alert(JSON.stringify(ret))
    }

    function echoAsyn() {
        // call function with namespace
        dsBridge.call("echo.asyn",{msg:" I am echoAsyn call",tag:2},function (ret) {
            alert(JSON.stringify(ret));
        })
    }

    function callProgress() {
        dsBridge.call("callProgress", function (value) {
            if(value==0) value="";
            document.getElementById("progress").innerText = value
        })
    }

    function hasNativeMethod(name) {
        alert(dsBridge.hasNativeMethod(name))
    }


  dsBridge.register('addValue', function (r, l) {
        return r + l;
    })

    dsBridge.registerAsyn('append', function (arg1, arg2, arg3, responseCallback) {
        responseCallback(arg1 + " " + arg2 + " " + arg3);
    })

    dsBridge.registerAsyn('startTimer', function (responseCallback) {
        var t = 0;
        var timer = setInterval(function () {
            if (t == 5) {
                responseCallback(t)
                clearInterval(timer)
            } else {
                // if the 2nd argument is false,  the java callback handler will be not removed!
                responseCallback(t++, false)
            }
        }, 1000)

    })

    // namespace test for syn functions
    dsBridge.register("syn", {
        tag: "syn",
        addValue:function (r,l) {
            return r+l;
        },
        getInfo: function () {
            return {tag: this.tag, value:8}
        }
    })

    // namespace test for asyn functions
    dsBridge.registerAsyn("asyn", {
        tag: "asyn",
        addValue:function (r,l, responseCallback) {
            responseCallback(r+l);
        },
        getInfo: function (responseCallback) {
            responseCallback({tag: this.tag, value:8})
        }
    })






























</script>
</body>
</html>
